﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.NonlinearEquationSolvers
{
    /// <summary>
    /// This class provides the root finding by using the Euler-Tschebyschow method.
    /// </summary>
    [Serializable]
    public class ApproximationEulerTschebyschowRootFinder : AbstractDerivativeNoNeedRootFinder
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationEulerTschebyschowRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public ApproximationEulerTschebyschowRootFinder(Polynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationEulerTschebyschowRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public ApproximationEulerTschebyschowRootFinder(SimplePolynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationEulerTschebyschowRootFinder"/> class.
        /// </summary>
        /// <param name="function">The function for finding the roots.</param>
        public ApproximationEulerTschebyschowRootFinder(IRealFunction function)
            : base(function)
        {
        }

        /// <summary>
        /// Find one root of the specified function by using the Euler-Tschebyschow method. The x has to
        /// be choose useful to find a root. The first and second derivation will be approximated by using
        /// extrapolation.
        /// </summary>
        /// <param name="x">The startvalue of the approximation.</param>
        /// <returns>One root of the specified function.</returns>
        public double FindRoots(double x)
        {
            return this.FindRoots(x, 1e-15, 1000);
        }

        /// <summary>
        /// Find one root of the specified function by using the Euler-Tschebyschow method. The x has to
        /// be choose useful to find a root. The first and second derivation will be approximated by using
        /// extrapolation.
        /// </summary>
        /// <param name="x">The startvalue of the approximation.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the specified function.</returns>
        public double FindRoots(double x, int iterations)
        {
            return this.FindRoots(x, 1e-15, iterations);
        }

        /// <summary>
        /// Find one root of the specified function by using the Euler-Tschebyschow method. The x has to
        /// be choose useful to find a root. The first and second derivation will be approximated by using 
        /// extrapolation.
        /// </summary>
        /// <param name="x">The startvalue of the approximation.</param>
        /// <param name="precision">The precision of the result.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the specified function.</returns>
        public double FindRoots(double x, double precision, int iterations)
        {
            double tempuri = 0;
            RealFirstDerivativeExtrapolationApproximator firstDerivation =
                new RealFirstDerivativeExtrapolationApproximator(this.Function);
            RealSecondDerivativeExtrapolationApproximator secondDerivation =
                new RealSecondDerivativeExtrapolationApproximator(this.Function);

            for (int i = 0; i < iterations; i++)
            {
                double fd = firstDerivation.ApproximateDerivative(x);
                double s = -(this.Function.SolveAt(x)/fd);
                double t = -0.5*((secondDerivation.ApproximateDerivative(x)*Math.Pow(s, 2))/fd);

                tempuri = x;
                x += s + t;

                if (Math.Abs(tempuri - x) < precision)
                {
                    this.NeededIterations = i;
                    this.PrecisionError = false;
                    this.RelativeError = Math.Abs(tempuri - x);

                    return tempuri;
                }
            }

            this.PrecisionError = true;
            this.NeededIterations = iterations;
            this.RelativeError = Math.Abs(tempuri - x);

            return x;
        }
    }
}